var util = require('../../utils/util.js')
var app = getApp()
Page({
  data: {
    count: 1,
    collection: 26,
    reading: 65,
    article: {},
    dataTodayData: "",
    dataOtherData: "",
    isEmpty: true
  },
  onLoad: function () {
    var dataTodayUrl = app.globalData.searchBase + "article/today?dev=1";
    var dataOtherUrl = app.globalData.searchBase + "article/day?dev=1&date=" + this.data.currDate;
    this.data.dataTodayUrl = dataTodayUrl;
    this.data.dataOtherUrl = dataOtherUrl;
    util.onRequest(dataTodayUrl, this.dealData);
    var randomNum = this.randomNum(1,461);
    this.setData({
      randomNum: randomNum
    })
    //设置默认字体大小
    wx.setStorage({
      key: 'fontSetting',
      data: 30

    })
    wx.setStorage({
      key: 'fontBgc',
      data: ''

    })

  },
  randomNum: function (minNum, maxNum) {

    return parseInt(Math.random() * (maxNum - minNum + 1) + minNum);


  },
  dealData: function (data) {
    var article = [];
    if (this.data.count === 16) {
      this.setData({
        count: 1
      })
    }
    var temp = {
      title: data.data.title,
      author: data.data.author,
      desc: data.data.digest,
      //id:data.data.wc
      curr: data.data.date.curr,
      prev: data.data.date.prev,
      avatar: this.data.count,
      randomNum: this.data.randomNum,
      collection: this.data.collection,
      reading: this.data.reading
    }
    article.push(temp)
    var totalActicle = {}

    //如果要绑定新加载的数据，那么需要同旧有的数据合并在一起
    if (!this.data.isEmpty) {
      totalActicle = this.data.article.concat(article);
    }
    else {
      totalActicle = article;
      this.data.isEmpty = false;
    }
    var currdate = article[0].curr;//设置下一天的文档
    this.setData({
      article: totalActicle,
      currDate: currdate
    });
    //console.log(article[0].curr)显示当前的日期
    // this.data.currDate = article[0].curr - 1;
    this.data.count += 1;
    this.data.randomNum = this.randomNum(1,461);
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh()
  },
  onReachBottom: function (event) {
    //拼接下一组数据的URL
    var currDate = this.data.currDate - 1;
    var nextUrl = app.globalData.searchBase + "article/day?dev=1&date=" + currDate;
    util.onRequest(nextUrl, this.dealData);

    //显示loading状态
    wx.showNavigationBarLoading();
    this.data.collection += 8;
    this.data.reading += 12;

  },

  onPullDownRefresh: function (event) {
    var currDate = this.data.currDate - 1;
    var refreshUrl = app.globalData.searchBase + "article/day?dev=1&date=" + currDate;
    //this.data.article = {};
    //this.data.isEmpty = true;
    util.onRequest(refreshUrl, this.dealData)
    wx.showNavigationBarLoading();
  },
  onPostTap: function (event) {
    var id = event.currentTarget.dataset.id;
    var count = event.currentTarget.dataset.count;
    var randomNum = event.currentTarget.dataset.randomNum;
    wx.navigateTo({
      url: "article-detail/article-detail?id=" + id + "&count=" + count + "&randomNum=" + randomNum
    })
  },
  onSwiper: function (event) {
    var swiperId = event.target.dataset.swiperId;
    wx.navigateTo({

      url: "article-detail/article-detail?swiperId=" + swiperId
    })


  }
})